package com.limbo.showapp.presenter;

import com.limbo.showapp.base.BasePresenter;
import com.limbo.showapp.bean.CartoonBean;
import com.limbo.showapp.bean.PictureBean;
import com.limbo.showapp.constant.AppConstant;
import com.limbo.showapp.model.CartoonModel;
import com.limbo.showapp.model.PictureModel;
import com.limbo.showapp.rxHttp.HttpObserver;
import com.limbo.showapp.view.IRefreshView;

/**
 * Created by wangqi on 2018/9/6.
 */

public class PicturePresenter extends BasePresenter<IRefreshView,PictureModel> {

    public int size = AppConstant.REFRESH_SIZE;
    public int page = 1;

    @Override
    protected PictureModel getModel() {
        return new PictureModel();
    }

    public void refreshData() {
        page = 1;
        getCartoonList();
    }

    public void loadMoreData() {
        page++;
        getCartoonList();
    }

    private void getCartoonList() {
        mModel.getPictureList(page, size, new HttpObserver<PictureBean>() {
            @Override
            protected void onHttpError(Throwable e) {
                page--;
                if (getView() != null)
                    getView().onError(e.getMessage());
            }

            @Override
            protected void onHttpSuccess(PictureBean bean) {
                if (getView() != null) {
                    if (page == 1) {
                        getView().onRefreshData(bean);
                    } else {
                        getView().onLoadMoreData(bean);
                    }
                }
            }
        });
    }
}
